package com.aps.util;

import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.aps.entity.eunm.TimeUnitType;

/**
 * Created by liaoguangan on 2017/6/8.
 */
public class DateUtil {

    public static void main(String args[]) {
        String str = "select 8 from group by ...";
        int groupIndex = str.lastIndexOf("group");
        System.out.print(str.substring(0, groupIndex));
    }

    /**
     * 获取近几天的日期
     * 负数表示前几天，正数表示后几天
     *
     * @param day
     * @return
     */
    public static List<Date> recentDays(int day) {
        Calendar cl = Calendar.getInstance();
        List<Date> recents = new ArrayList<Date>();
        //放入今天的日期
        recents.add(cl.getTime());
        int flag = day > 0 ? 1 : -1;
        for (int i = 1; i < Math.abs(day); i++) {
            cl.set(Calendar.DAY_OF_YEAR, cl.get(Calendar.DAY_OF_YEAR) + flag);
            recents.add(cl.getTime());
        }
        return recents;
    }

    /**
     * 获取近几天的日期
     * 负数表示前几天，正数表示后几天
     *
     * @param day
     * @return
     */
    public static Map<Integer, Date> recentDaysAsMap(int day) {
        Calendar cl = Calendar.getInstance();
        Map ret = new HashMap();
        //放入今天的日期
        int flag = day > 0 ? 1 : -1;
        for (int i = 0; i < Math.abs(day); i++) {
            cl.set(Calendar.DAY_OF_YEAR, cl.get(Calendar.DAY_OF_YEAR) + flag);
            ret.put(flag * i, cl.getTime());
        }
        return ret;
    }

    /**
     * 获取近几月的日期
     *
     * @param month
     * @return
     */
    public static List<Date> recentMonths(int month) {
        Calendar cl = Calendar.getInstance();
        List<Date> recents = new ArrayList<Date>();
        //日期设置为一号
        cl.set(Calendar.DAY_OF_MONTH, 1);
        //放入当月的日期
        recents.add(cl.getTime());
        int flag = month > 0 ? 1 : -1;
        for (int i = 1; i < Math.abs(month); i++) {
            cl.set(Calendar.MONTH, cl.get(Calendar.MONTH) + flag);
            recents.add(cl.getTime());
        }
        return recents;
    }

    /**
     * 格式化日期
     *
     * @param date
     * @param style
     * @return
     */
    public static String formatDate(Date date, String style) {
        style = style == null ? "yyyy-MM-dd" : style;
        SimpleDateFormat sd = new SimpleDateFormat(style);
        return sd.format(date);
    }

    /**
     * 字符串转日期
     *
     * @param d
     * @param style
     * @return
     */
    public static Date parseDate(String d, String style) {
        style = style == null ? "yyyy-MM-dd" : style;
        SimpleDateFormat sd = new SimpleDateFormat(style);
        try {
            return sd.parse(d);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 判断字符串是否为日期
     *
     * @param str
     * @return
     */
    public static Object judgeString(String str) {
        if (str.contains("-") || str.contains(":")) {
            String style = "yyyy-MM-dd HH:mm:ss";
            if (str.length() == 10) {
                style = "yyyy-MM-dd";
            }
            SimpleDateFormat sd = new SimpleDateFormat(style);
            try {
                return sd.parse(str);
            } catch (ParseException e) {
                //                e.printStackTrace();
            }
        }
        return str;
    }

    /**
     * 判断字符串是否为数字
     *
     * @param str
     * @return
     */
    public static Object isNumeric(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return str;
            }
        }
        try {
            return NumberFormat.getInstance().parse(str);
        } catch (ParseException e) {
            return str;
        }
    }

    /**
     * 计算分钟差
     *
     * @param fromDate
     * @param toDate
     * @return
     */
    public static int devideMunitesTime(Date fromDate, Date toDate) {
        long from = fromDate.getTime();
        long to = toDate.getTime();
        return (int) (to - from) / (1000 * 60);
    }

    public static Date getDate(Calendar calendar, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String dateString = sdf.format(calendar.getTime());
        try {
            return sdf.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 清除日期的时间部分
     * @param d
     * @return
     */
    public static Date clearDateTime(Date d) {
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);

        return c.getTime();
    }

    public static Date addTime(TimeUnitType type, Integer time) {

        Calendar cl = Calendar.getInstance();
        cl.add(Calendar.SECOND, transformTime(type, time));
        return cl.getTime();
    }

    public static Date addTime(Date date, TimeUnitType type, Integer time) {

        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        cl.add(Calendar.SECOND, transformTime(type, time));
        return cl.getTime();
    }

    /**
     * 转换成秒
     * @param type
     * @param time
     * @return
     */
    public static Integer transformTime(TimeUnitType type, Integer time) {
        switch (type) {
            case H: {
                return time * 60 * 60;
            }
            case M: {
                return time * 60;
            }
            case S:
            default: {
                return time;
            }
        }
    }

}
